//
//  HNSH_UploadAppAlert.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/10.
//

#import "HNSH_UploadAppAlert.h"
#import "HNSH_PopupController.h"

@interface HNSH_UploadAppAlert ()
/* nyPopView */
@property(nonatomic, strong) HNSH_PopupController *nyPopView;

/* bgView */
@property (nonatomic, strong) UIView *bgView;
/* imgV */
@property (nonatomic, strong) UIImageView *imgV;
/* titleLabel */
@property (nonatomic, strong) UILabel *titleLabel;
/* desLabel */
@property (nonatomic, strong) UILabel *desLabel;
/* closeButton */
@property (nonatomic, strong) UIButton *closeButton;
@end

@implementation HNSH_UploadAppAlert

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self configUI];
    }
    return self;
}

- (void)showView {
    self.nyPopView = [[HNSH_PopupController alloc]init];
    self.nyPopView.dismissOnMaskTouched = NO;
    self.nyPopView.layoutType = PopupLayoutTypeCenter;
    self.nyPopView.slideStyle = PopupSlideStyleFade;
    [self.nyPopView presentContentView:self duration:0.25 springAnimated:YES inView:kSTWindow];
//    self.nyPopView.popupView.layer.cornerRadius = 16.0f;
//    self.nyPopView.popupView.layer.masksToBounds = YES;
}

- (void)closeView {
    [self.nyPopView dismissWithDuration:0.25 springAnimated:YES];
}

- (void)setDesString:(NSString *)desString {
    _desString = desString;
    self.desLabel.text = desString;
}

- (void)configUI {
    [self addSubview:self.bgView];
    [self addSubview:self.imgV];
    [self.bgView addSubview:self.titleLabel];
    [self.bgView addSubview:self.desLabel];
    [self.bgView addSubview:self.closeButton];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(44);
    }];
    
    [self.imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.mas_equalTo(0);
        make.width.height.mas_equalTo(88);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(64);
        make.height.mas_equalTo(22);
    }];
    
    [self.desLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(92);
        make.right.mas_equalTo(-30);
        make.left.mas_equalTo(30);
    }];
    
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(23);
        make.right.mas_equalTo(-23);
        make.bottom.mas_equalTo(-16);
        make.height.mas_equalTo(44);
    }];
}

- (void)confrimAction{
    [HNSH_UserNetServer requestAPPProductDownloadUrlSuccess:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            NSDictionary * data = responseObject.resultset;
            NSString * download_url = data[@"download_url"];
            NSURL * url = [NSURL URLWithString:download_url];
            [UIApplication.sharedApplication openURL:url options:@{} completionHandler:^(BOOL success) {
                if (success) {
                    [self closeView];
                }
            }];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark lazy
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = [UIColor appWhiteLabelColor];
        _bgView.layer.cornerRadius = 8.0f;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}

- (UIImageView *)imgV {
    if (!_imgV) {
        _imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"base_icon_update"]];
    }
    return _imgV;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = [UIColor appBlack333LabelColor];
        _titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _titleLabel.text = @"版本更新";
    }
    return _titleLabel;
}

- (UILabel *)desLabel {
    if (!_desLabel) {
        _desLabel = [[UILabel alloc]init];
        _desLabel.textColor = [UIColor colorWithHexString:@"#999999"];
        _desLabel.font = [UIFont systemFontOfSize:13];
        _desLabel.numberOfLines = 0;
    }
    return _desLabel;
}

- (UIButton *)closeButton {
    if (!_closeButton) {
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeButton setTitle:@"立即更新" forState:UIControlStateNormal];
        [_closeButton setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
        _closeButton.backgroundColor = [UIColor appGreenColor];
        _closeButton.layer.cornerRadius = 22.0f;
        _closeButton.layer.masksToBounds = YES;
        __weak __typeof(&*self)weakSelf = self;
        [[_closeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf confrimAction];
        }];
    }
    return _closeButton;
}

@end
